<?php
/**
* 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Datagram
 * @subpackage G
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: SQLMultiSeriesPivot.php 7 2009-11-14 22:42:08Z nkeyes $
 */
class Bushido_Datagram_SQLMultiSeriesPivot extends Bushido_Datagram_Abstract
{
	/**
	 * The name of this chart
	 * 
	 * @var string
	 */
	protected $chartDisplayName ='';
	protected $_seriesName;
	protected $_sql;
	protected $_dbh;
	
	
	/**
	 * 
	 * @param $sql
	 * @param $dbh
	 * @param $configOptions
	 * @return void
	 */
	public function __construct($sql, $dbh, $configOptions = array())
	{
		$this->allowedConfigOptions = array('id', 'caption');
		$this->config = new Bushido_Configurable();
		$this->config->addAllowedConfigOptions($this->allowedConfigOptions);
		$this->config->setConfigOptions($configOptions);
		
		$this->setSql($sql);
		$this->setDbh($dbh);
		
		parent::__construct($configOptions);
	}
	
	/**
	 * 
	 * @return string The name of this chart
	 */
	public function getChartDisplayName()
	{
		return $this->chartDisplayName;
	}
	
	/**
	 * Sets the raw data for this chart
	 * @return void
	 */
	protected function _setRawData()
	{
		$resultSet = $this->getDbh()->fetchAll($this->getSql());
		
		$columnList = array();
		$seriesList = array();
		
		foreach($resultSet as $row)
		{
			$categoryName = array_shift($row);
			foreach($row as $field => $value)
			{
				$seriesList[$field][$categoryName] = $value;
			}
			unset($field);
			unset($value);
		}
		unset($row);
		
		foreach($seriesList as $seriesName => $seriesData)
		{
			$this->setSeries($seriesName, $seriesData);
		}
		unset($seriesName);
		unset($seriesData);
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getSql()
	{
		return $this->_sql;
	}
	
	
	/**
	 * 
	 * @param $sql
	 * @return void
	 */
	public function setSql($sql)
	{
		$this->_sql = $sql;
	}
	
	
	/**
	 * 
	 * @return Zend_Db_Adapter_Abstract
	 */
	public function getDbh()
	{
		return $this->_dbh;
	}
	
	
	/**
	 * 
	 * @param $dbh
	 * @return void
	 */
	public function setDbh($dbh)
	{
		$this->_dbh = $dbh;
	}
}